﻿using Newtonsoft.Json.Linq;
using NLog;
using Persistence.Core;
using QueryDataLogic.Core;
using System;
using System.Data.SqlClient;

namespace PersistenceDataLogic.Core
{
    public class CheckMeal
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        BasicInfo bi = new BasicInfo();
        QueryData qd = new QueryData();
        PersistenceData pd = new PersistenceData();

        private JObject GetParam(string url)
        {
            JObject joParam = new JObject();
            try
            {
                string[] paramList = url.Split('?')[1].Split('&');
                string[] paramItem;
                foreach (string param in paramList)
                {
                    paramItem = param.Split('=');
                    joParam[paramItem[0]] = paramItem[1];
                }
            }
            catch
            {
                throw;
            }
            return joParam;
        }

        private JObject GetMealInfo(string url, out string dbName)
        {
            JObject result = new JObject();
            JObject joParam = new JObject();
            dbName = "";
            try
            {
                joParam = GetParam(url);
                int districtId = (int)joParam["districtId"];
                string number = (string)joParam["number"];
                dbName = bi.GetDBName(districtId);

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "SELECT mr.type,mr.id,mr.mealId,mr.mealName,mr.qty,mr.remark,mr.startTime,mr.endTime,mu.name FROM GBT_View_MealsReport mr " +
                    "inner join MealUser mu on mr.UserID = mu.UserID " +
                    "WHERE mr.mid = @number "
                };

                comm.Parameters.AddWithValue("@number", number);
                JArray jaResult = qd.GetData(comm, dbName);
                result = (JObject)jaResult[0];
            }
            catch
            {
                throw;
            }
            return result;
        }

        public JObject Check(JObject joParam)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";
                string url = (string)joParam["url"];
                JObject joMealInfo = GetMealInfo(url, out dbName);
                int id = (int)joMealInfo["id"];
                int type = (int)joMealInfo["type"];
                int mealId = (int)joMealInfo["mealId"];
                string mealName = (string)joMealInfo["mealName"];
                int qty = (int)joMealInfo["qty"];
                string typeName = (string)joMealInfo["remark"];
                string name = (string)joMealInfo["name"];
                DateTime dtStartTime = (DateTime)joMealInfo["startTime"];
                DateTime dtEndTime = (DateTime)joMealInfo["endTime"];
                DateTime currentTime = DateTime.Now;
                int result = 0;

                SqlCommand comm = new SqlCommand();
                if (type == 0)
                {
                    comm.CommandText = "UPDATE ReportMeals SET IsTakeMeals = 1 WHERE id = @id ";
                }
                else
                {
                    comm.CommandText = "UPDATE IncrementalMeal SET IsTakeMeals = 1 WHERE id = @id ";
                }
                comm.Parameters.AddWithValue("@id", id);
                if (currentTime >= dtStartTime && currentTime <= dtEndTime)
                {
                    result = pd.ExeSqlCode(comm, dbName);
                    joResult["data"] = result;
                    joResult["state"] = true;
                    joResult["message"] = name + " " + typeName + " " + mealName + " " + qty.ToString() + " 份，取餐成功";
                }
                else
                {
                    joResult["data"] = result;
                    joResult["state"] = false;
                    joResult["message"] = "取餐失败";
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.WriteLine(ex);
                joResult["data"] = 0;
                joResult["state"] = false;
                joResult["message"] = "取餐失败";
            }
            return joResult;
        }
    }
}
